﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;

namespace Baosight.ZNH.ZMCA.UI.Controls
{
    public partial class DropDownMultiCheckBox : UserControl
    {

        #region 变量定义

        public static readonly int itemCountPerRow = 4;
        private IList<CheckBox> lstCheckedItems = new List<CheckBox>();
        private IList<CheckBox> lstAllItems = new List<CheckBox>();
        private CheckBox chkAll = null;     //控件的“全部”勾选框
        private bool IsCheckAllItem = true; //设置控件的“全部”勾选框是否勾选

        public delegate void MyEventHandler(object sender, object e);
        public event MyEventHandler DropDownClosed;

        #endregion

        #region 对外提供接口

        /// <summary>
        /// 所有选中的CheckBox
        /// </summary>
        public IList<CheckBox> LstCheckedItems
        {
            get
            {
                return lstCheckedItems;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DropDownMultiCheckBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 绑定List<EIRow>的值到控件中
        /// </summary>
        /// <param name="list"></param>
        /// <param name="index"></param>
        public void BindItemsToGrid(IList<EIRow> list, string index)
        {
            this.grdCheckbox.Children.Clear();
            lstCheckedItems.Clear();
            lstAllItems.Clear();

            int count = list.Count;

            if (count > 0)
            {
                if (count > itemCountPerRow)
                {
                    if (count % itemCountPerRow == 0)     //每行保存4个条目
                    {
                        DivRowAndColumn(count / itemCountPerRow, 5);    //条目数为4的整数倍
                    }
                    else
                    {
                        DivRowAndColumn(count / itemCountPerRow + 1, 5);    //条目数不为4的整数倍，多添加一行
                    }
                }
                else        //默认划分为1行4列
                {
                    DivRowAndColumn(1, 5);
                }

                AddAllItemsToGrid(list, index);
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 获取选中的List<CheckBox>的EIRow
        /// </summary>
        /// <returns></returns>
        public IList<EIRow> GetCheckedItems()
        {
            IList<EIRow> list = new List<EIRow>();

            if (this.lstCheckedItems.Count > 0)
            {
                for (int i = 0; i < this.lstCheckedItems.Count; i++)
                {
                    EIRow row = lstCheckedItems[i].DataContext as EIRow;

                    list.Add(row);
                }

                return list;

            }
            else
            {
                return list;
            }
        }

        /// <summary>
        /// 设置控件显示的内容
        /// </summary>
        /// <param name="content"></param>
        public void SetContent(string content)
        {
            this.dropDownButton.Content = content;
        }

        /// <summary>
        /// 设置选中全部分析项时“全部”勾选框是否勾选上
        /// </summary>
        /// <param name="flag"></param>
        public void SetAllCheckBoxChecked(bool flag)
        {
            this.IsCheckAllItem = flag;
        }

        /// <summary>
        /// 判断是否是全选
        /// </summary>
        public bool IsAllChecked()
        {
            if (this.chkAll.IsChecked == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 控件的显示/隐藏接口
        /// </summary>
        /// <param name="show"></param>
        public void IsShow(bool show)
        {
            if (show)
            {
                this.dropDownButton.Visibility = Visibility.Visible;
            }
            else
            {
                this.dropDownButton.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

        #region 添加数据部分

        /// <summary>
        /// 将指定的Grid划分成行列
        /// </summary>
        /// <param name="gridName"></param>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        private void DivRowAndColumn(int rowCount, int colCount)
        {
            if (rowCount <= 0 || colCount <= 0)
            {
                rowCount = 1;
                colCount = 1;
            }
            else
            {
                for (int i = 0; i < rowCount; i++)
                {
                    grdCheckbox.RowDefinitions.Insert(i, new RowDefinition() { Height = GridLength.Auto });
                }
                for (int j = 0; j < colCount; j++)
                {
                    grdCheckbox.ColumnDefinitions.Insert(j, new ColumnDefinition() { Width = GridLength.Auto });
                }
            }
        }

        /// <summary>
        /// 添加所有项目到Grid中（包含“全部”勾选框）
        /// </summary>
        /// <param name="gridName"></param>
        /// <param name="list"></param>
        /// <param name="index"></param>
        private void AddAllItemsToGrid(IList<EIRow> list, string index)
        {
            int rowIndex = 0;
            int colIndex = 0;

            if (list.Count > 0)     //添加“全部”选择框
            {
                StackPanel stack = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    Margin = new Thickness(5, 5, 5, 5)
                };

                CheckBox check = new CheckBox() { DataContext = list, Tag = "ALL" };
                check.IsChecked = true;
                check.Content = "全部";

                chkAll = check;
                check.Click += new RoutedEventHandler(CheckBoxItemClick);

                stack.Children.Add(check);

                stack.SetValue(Grid.RowProperty, rowIndex);
                stack.SetValue(Grid.ColumnProperty, colIndex++);
                this.grdCheckbox.Children.Add(stack);
            }
            else
            {
                return;
            }

            AddItemsToGrid(list, index);
        }

        /// <summary>
        /// 添加查询到的项目到Grid中（不包含“全部”勾选框）
        /// </summary>
        /// <param name="gridName"></param>
        /// <param name="list"></param>
        /// <param name="index"></param>
        private void AddItemsToGrid(IList<EIRow> list, string index)
        {
            int rowIndex = 0;
            int colIndex = 1;

            this.lstAllItems.Clear();
            this.lstCheckedItems.Clear();

            for (int i = 0; i < list.Count; i++)
            {
                StackPanel stack = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    Margin = new Thickness(5, 5, 5, 5)
                };

                CheckBox check = new CheckBox() { DataContext = list[i], Tag = "" };
                check.IsChecked = true;
                check.Content = list[i][index].ToString().Trim();

                check.Click += new RoutedEventHandler(CheckBoxItemClick);
                this.lstAllItems.Add(check);
                this.lstCheckedItems.Add(check);


                stack.Children.Add(check);
                stack.SetValue(Grid.RowProperty, rowIndex);
                stack.SetValue(Grid.ColumnProperty, colIndex++);
                this.grdCheckbox.Children.Add(stack);

                if (colIndex == 5)
                {
                    colIndex = 1; rowIndex++;
                }
            }
        }

        #endregion

        #region 事件响应部分

        /// <summary>
        /// 勾选事件响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBoxItemClick(object sender, RoutedEventArgs e)
        {
            CheckBox selectedBox = sender as CheckBox;

            if (selectedBox == null)
            {
                return;
            }

            if (selectedBox.IsChecked == false)
            {
                if (selectedBox.Tag.Equals("ALL"))
                {
                    lstCheckedItems.Clear();

                    for (int i = 0; i < this.lstAllItems.Count; i++)
                    {
                        lstAllItems[i].IsChecked = false;
                    }

                }
                else
                {
                    lstCheckedItems.Remove(selectedBox);

                    if (chkAll.IsChecked == true)
                    {
                        chkAll.IsChecked = false;
                    }
                }
            }
            else
            {
                if (selectedBox.Tag.Equals("ALL"))
                {
                    lstCheckedItems.Clear();

                    for (int i = 0; i < this.lstAllItems.Count; i++)
                    {
                        lstAllItems[i].IsChecked = true;
                        lstCheckedItems.Add(lstAllItems[i]);
                    }
                }
                else
                {
                    lstCheckedItems.Add(selectedBox);

                    if ((lstCheckedItems.Count == lstAllItems.Count) && (chkAll.IsChecked == false) && (IsCheckAllItem))
                    {
                        chkAll.IsChecked = true;
                    }

                }
            }
        }

        /// <summary>
        /// 下拉框关闭事件响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dropDownButton_DropDownClosed(object sender, RoutedEventArgs e)
        {
            if (this.chkAll.IsChecked == true)
            {
                this.dropDownButton.Content = "全部";
            }
            else
            {
                string selectedItemNames = string.Empty;
                foreach (var item in lstCheckedItems)
                {
                    if (selectedItemNames == string.Empty)
                    {
                        selectedItemNames = item.Content.ToString();
                    }
                    else
                    {
                        selectedItemNames = selectedItemNames + ", " + item.Content.ToString();
                    }
                }
                this.dropDownButton.Content = selectedItemNames;
                //this.dropDownButton.Content = "选择了" + this.checkedTypeBoxes.Count.ToString() + "项";
            }
            dropDownButton.SetValue(ToolTipService.ToolTipProperty, this.dropDownButton.Content);

            if (DropDownClosed != null)
            {
                DropDownClosed(this, null);
            }
        }

        #endregion

    }
}
